using System.Collections;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class Oil : MonoBehaviour
{
	public bool EcoMode;

	public DOTweenAnimation EcoParent;

	public Image EcoBar;

	public Image EcoVisual;

	private int _currentLevel;

	[SerializeField]
	private RectTransform _sliderImage;

	[SerializeField]
	private Image _fillImage;

	[SerializeField]
	private Image _bonusFillImage;

	[SerializeField]
	private GameObject _lowOil;

	[SerializeField]
	private RectTransform _gaugeParent;

	public Vector2 Smallest;

	public Vector2 Biggest;

	private float CurrentOilValue;

	private float CurrentBonusOilValue;

	public MoveDirection joystick;

	private BubbleBonusManager _bubbleBonusManager;

	private float step = 1f;

	public Item MaxOilUpgrade;

	private bool _spendingOil;

	private float _spendTimer;

	public Transform _oilImage;

	private int CurrentOilLevel;

	private int lastUpgradeLevel = -1;

	public float MaxOil { get; private set; }

	public float OilValue => CurrentOilValue;

	private void Start()
	{
		Smallest = _gaugeParent.rect.size;
		Biggest = Smallest;
		Biggest.y *= 2f;
		UpdateValues();
		EcoParent.gameObject.SetActive(value: false);
		EcoVisual.gameObject.SetActive(value: false);
		joystick = SingletonMB<GameManager>.Instance.Joystick;
	}

	public void UpdateValues()
	{
		_currentLevel = NintendoPlayerPrefs.GetInt("Oil", 0);
		if (SingletonMB<DuelModeManager>.Instance != null)
		{
			_currentLevel = 60 + SingletonMB<Shop>.Instance.MaxOilLevel * 5 + InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) * 5;
		}
		CurrentOilLevel = _currentLevel;
		CurrentOilValue = 60 + _currentLevel * 5;
		if (_bubbleBonusManager != null)
		{
			CurrentBonusOilValue = CurrentOilValue * (_bubbleBonusManager.fuelMultiplicator + (float)NintendoPlayerPrefs.GetInt("ScinOil", 0) / 100f);
		}
		MaxOil = CurrentOilValue;
		if (Preloader.ShopConfig.UpgradeShop)
		{
			Vector2 endValue = Vector2.Lerp(Smallest, new Vector2(36.77f, 158.33f), (float)InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) / (float)MaxOilUpgrade.Max);
			if (lastUpgradeLevel > 0)
			{
				float startTime = Time.time;
				float startMax = 60 + SingletonMB<Shop>.Instance.MaxOilLevel * 5 + lastUpgradeLevel * 5;
				MaxOil = startMax;
				float targetMax = 60 + SingletonMB<Shop>.Instance.MaxOilLevel * 5 + InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) * 5;
				if (MaxOil < CurrentBonusOilValue)
				{
					CurrentBonusOilValue = MaxOil;
				}
				_gaugeParent.DOSizeDelta(endValue, 1f, snapping: true).OnUpdate(delegate
				{
					MaxOil = Mathf.Lerp(startMax, targetMax, Time.time - startTime);
					_fillImage.fillAmount = CurrentOilValue / MaxOil;
					_bonusFillImage.fillAmount = CurrentBonusOilValue / MaxOil;
				}).OnComplete(delegate
				{
					MaxOil = targetMax;
					_fillImage.fillAmount = CurrentOilValue / MaxOil;
					_bonusFillImage.fillAmount = CurrentBonusOilValue / MaxOil;
				})
					.SetEase(Ease.Linear);
				lastUpgradeLevel = InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade);
				_lowOil.SetActive(CurrentOilValue < 15f);
			}
			else
			{
				_gaugeParent.DOSizeDelta(endValue, 0.001f, snapping: true);
				lastUpgradeLevel = InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade);
				MaxOil = 60 + SingletonMB<Shop>.Instance.MaxOilLevel * 5 + InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) * 5;
				_lowOil.SetActive(CurrentOilValue < 15f);
				_fillImage.fillAmount = CurrentOilValue / MaxOil;
				if (MaxOil < CurrentBonusOilValue)
				{
					CurrentBonusOilValue = MaxOil;
				}
				_bonusFillImage.fillAmount = CurrentBonusOilValue / MaxOil;
			}
		}
		else
		{
			MaxOil = CurrentOilValue;
			_lowOil.SetActive(CurrentOilValue < 15f);
			_fillImage.fillAmount = CurrentOilValue / MaxOil;
			if (MaxOil < CurrentBonusOilValue)
			{
				CurrentBonusOilValue = MaxOil;
			}
			_bonusFillImage.fillAmount = CurrentBonusOilValue / MaxOil;
		}
	}

	public void RefillAfterMiniGame()
	{
		float num = 60 + SingletonMB<Shop>.Instance.MaxOilLevel * 5 + InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) * 5;
		CurrentOilValue = 0.15f * num;
		_fillImage.fillAmount = CurrentOilValue / num;
	}

	public void StopSpend()
	{
		if (!(SingletonMB<DuelModeManager>.Instance != null))
		{
			StopAllCoroutines();
			_spendingOil = false;
			EcoParent.gameObject.SetActive(value: false);
			EcoVisual.gameObject.SetActive(value: false);
		}
	}

	public void StartSpendingOil()
	{
		joystick = SingletonMB<GameManager>.Instance.Joystick;
		if (!Preloader.OptimizationConfig.IsEnabled)
		{
			StopSpend();
			StartCoroutine(SpendOil());
		}
		else
		{
			_spendingOil = true;
			_spendTimer = 0.3f;
		}
		_sliderImage.localScale = Vector3.one;
	}

	private IEnumerator SpendOil()
	{
		while (CurrentOilValue > 0f)
		{
			Debug.Log("Eco Mode");
			if (Singleton<InputController>.Instance.MoveVector != Vector3.zero || Singleton<InputController>.Instance.RightPressed || Singleton<InputController>.Instance.LeftPressed || Singleton<InputController>.Instance.DownPressed || Singleton<InputController>.Instance.UpPressed)
			{
				int num = (Singleton<InputController>.Instance.RightPressed ? 1 : (Singleton<InputController>.Instance.LeftPressed ? (-1) : 0));
				int num2 = (Singleton<InputController>.Instance.UpPressed ? 1 : (Singleton<InputController>.Instance.DownPressed ? (-1) : 0));
				Vector3 normalized = (new Vector3(num, num2, 0f) + Singleton<InputController>.Instance.MoveVector).normalized;
				step = 0.5f * Vector2.Distance(Vector2.zero, new Vector2(normalized.x, normalized.y));
			}
			else
			{
				step = Vector2.Distance(Vector2.zero, new Vector2(joystick.Direction.x, joystick.Direction.y));
			}
			if (Preloader.ShopConfig.UpgradeShop)
			{
				step *= 1f + ((float)InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) + (float)CurrentOilLevel) * 0.01f;
			}
			if (CurrentBonusOilValue > CurrentOilValue)
			{
				CurrentBonusOilValue -= step;
				if (SingletonMB<BonusLevelManager>.Instance.ForceBonusLevel)
				{
					CurrentBonusOilValue -= step * 10f;
				}
				_bonusFillImage.fillAmount = CurrentBonusOilValue / MaxOil;
				yield return new WaitForSeconds(0.3f);
				continue;
			}
			CurrentBonusOilValue = -1f;
			_bonusFillImage.fillAmount = -1f;
			if (Preloader.RailDigging.IsEnabled)
			{
				CurrentOilValue += step / 4f;
			}
			CurrentOilValue -= step;
			if (SingletonMB<BonusLevelManager>.Instance.ForceBonusLevel)
			{
				CurrentOilValue -= step * 10f;
			}
			_fillImage.fillAmount = CurrentOilValue / MaxOil;
			_lowOil.SetActive(CurrentOilValue < 15f);
			yield return new WaitForSeconds(0.3f);
		}
		Object.FindObjectOfType<LostFuelScreen>().FuelLost();
	}

	public void AddOil(int value)
	{
		if (CurrentOilValue + (float)value >= (float)(60 + _currentLevel * 5))
		{
			UpdateValues();
		}
		else
		{
			CurrentOilValue += value;
		}
	}

	public void AnimateOil()
	{
		base.transform.DOPunchScale(Vector3.one * 1.1f, 0.5f, 1);
	}

	private void Update()
	{
		if (!_spendingOil)
		{
			return;
		}
		if (SingletonMB<GameManager>.Instance.EcoMode)
		{
			EcoParent.gameObject.SetActive(value: true);
			EcoVisual.gameObject.SetActive(value: true);
			EcoParent.transform.localScale = Vector3.one;
			EcoBar.fillAmount = SingletonMB<GameManager>.Instance.EcoTimer / 30f;
			SingletonMB<GameManager>.Instance.EcoTimer -= Time.deltaTime;
			if (SingletonMB<GameManager>.Instance.EcoTimer <= 0f)
			{
				SingletonMB<GameManager>.Instance.EcoMode = false;
				EcoParent.DORestart();
				EcoParent.DOPlay();
				EcoVisual.gameObject.SetActive(value: false);
			}
		}
		else if (CurrentOilValue > 0f)
		{
			if (SingletonMB<RvDrillBoost>.Instance.bonusActive)
			{
				return;
			}
			if (Time.timeScale > 0f && Time.unscaledDeltaTime < 3f)
			{
				_spendTimer += Time.unscaledDeltaTime;
			}
			if (_spendTimer < 0.3f)
			{
				return;
			}
			_spendTimer -= 0.3f;
			if (Singleton<InputController>.Instance.MoveVector != Vector3.zero || Singleton<InputController>.Instance.RightPressed || Singleton<InputController>.Instance.LeftPressed || Singleton<InputController>.Instance.DownPressed || Singleton<InputController>.Instance.UpPressed)
			{
				int num = (Singleton<InputController>.Instance.RightPressed ? 1 : (Singleton<InputController>.Instance.LeftPressed ? (-1) : 0));
				int num2 = (Singleton<InputController>.Instance.UpPressed ? 1 : (Singleton<InputController>.Instance.DownPressed ? (-1) : 0));
				Vector3 normalized = (new Vector3(num, num2, 0f) + Singleton<InputController>.Instance.MoveVector).normalized;
				step = 0.8f * Vector2.Distance(Vector2.zero, new Vector2(normalized.x, normalized.y));
			}
			else
			{
				step = Vector2.Distance(Vector2.zero, new Vector2(joystick.Direction.x, joystick.Direction.y));
			}
			if (Preloader.ShopConfig.UpgradeShop)
			{
				step *= 1f + ((float)InventoryManager.Instance.GetInventoryCount(MaxOilUpgrade) + (float)CurrentOilLevel) * 0.01f;
			}
			if (CurrentBonusOilValue > CurrentOilValue)
			{
				CurrentBonusOilValue -= step;
				if (SingletonMB<BonusLevelManager>.Instance.ForceBonusLevel)
				{
					CurrentBonusOilValue -= step * 10f;
				}
				_bonusFillImage.fillAmount = CurrentBonusOilValue / MaxOil;
				return;
			}
			CurrentBonusOilValue = -1f;
			_bonusFillImage.fillAmount = -1f;
			CurrentOilValue -= step;
			if (SingletonMB<BonusLevelManager>.Instance.ForceBonusLevel)
			{
				CurrentOilValue -= step * 10f;
			}
			_fillImage.fillAmount = CurrentOilValue / MaxOil;
			_lowOil.SetActive(CurrentOilValue < 15f);
		}
		else
		{
			_spendingOil = false;
			Object.FindObjectOfType<LostFuelScreen>().FuelLost();
		}
	}
}
